<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns:head="http://www.w3.org/1999/html">
<head th:include="public/public_css::public_css ('人工驱动补货')" />

<body style="background-color: #f2f2f2;">
<div class="layui-fluid larry-wrapper">
    <div class="layui-card">
        <div class="layui-row layui-col-space30">
        <div class="layui-col-xs24">
                <form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm">
                    <div class="layui-inline">
                        <label class="layui-form-label">补货单号</label>
                        <input type="text" class="layui-input" name="bh" id="bh" placeholder="请输入补货单编号" />

                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="time" id="cjsj" placeholder=" - ">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">补货状态</label>
                        <select name="zt">
                            <option value="">全部</option>
                            <option value="0">初始</option>
                            <option value="1">已生成</option>
                            <option value="2">已确认</option>
                            <option value="3">作废</option>
                        </select>
                    </div>
                    <div class="layui-inline inline-search-btn">
                        <a class="layui-btn layui-btn-normal" onclick="queryTable(1)" id="query" data-type="reload">查询</a>
                    </div>
                    <div class="layui-inline inline-reset-btn">
                        <button type="reset" onclick="queryTable(0)" class="layui-btn layui-btn-normal">重置</button>
                    </div>
                </form>
            <!--顶部按钮展示区域 【 tpl-button 】 -->
            <div class="group-button tpl-button" id="toolbar"></div>
            <!--列表-->
            <section class="panel panel-padding layui-card-body">
                <div class="pro">
                    <table id="table" class="table text-nowrap"></table>
                </div>
            </section>
        </div>
    </div>
    </div>
</div>

</body>


<div th:include="public/public_js::public_js"></div>
<div th:include="public/public_btn::public_btn"></div>
<style type="text/css">
</style>
<script th:inline="javascript">
    var functions = [[${functions}]];
    layui.use(['list', 'layer', 'zzpUtils', 'jquery', 'laydate', 'tablebutton'], function () {
        var zzpUtils= layui.zzpUtils,
            $ = layui.jquery,
            laydate = layui.laydate,
            tablebtn = layui.tablebutton;
        tablebtn.buildOnClick({
            type: 1,
            temp: $("#function_tpl"),
            functions: functions.headBtn,
            row: null,
            event: {
                searchEvent: function(){
                    zzpUtils.refreshTable("#table");
                },
                /*新增*/
                addEvent: function () {
                    add();
                },
                /*查看*/
                getEvent: function (data) {
                    getDialog(data);
                },
                /*查看补货任务*/
                searchBhEvent: function (data) {
                    getBhrwDialog(data);
                },
                /*编辑*/
                editEvent: function (data) {
                    if (data.row != undefined) {
                        upDialog(data);
                    }
                },
                /*生成补货单*/
                generateBhd: function (data) {
                    generateBhdDialog();
                },
                /*取消补货*/
                cancelBh: function (data) {
                    cancelBhDialog(data);
                },
                /*生成*/
                generateEvent: function (data) {
                    generateDialog(data);
                },
                /*删除*/
                deleteEvent: function (data) {
                    if (data.row != undefined) {
                        var ids = [];
                        ids.push(data.row.id)
                        delDialog(ids);
                    } else {
                        var arr = [];
                        var $rows = $table.bootstrapTable('getSelections');
                        if ($rows.length > 0) {
                            arr = $.map($rows, function (row) {
                                return row.id;
                            })
                            delDialog(arr);
                        }else{
                            layer.msg("至少选择一条数据");
                        }
                    }
                },
                /*导出*/
                exportEvent: function (data) {
                    var arr = [];
                    var $rows = $table.bootstrapTable('getSelections');
                    if ($rows.length > 0) {
                        arr = $.map($rows, function (row) {
                            return row.id;
                        })
                    }
                    /*
                     * 以下是将ids和对象拼接在一起
                     * */
                    var str = zzpUtils.getFormJson($("#searchForm"));
                    var object2 = Object.assign({ids: arr}, str);
                    /*将条件查询和ids传到后台*/
                    zzpUtils.outputXslx({
                        url: "/wmsartificialreplenishment/getOutPutList",
                        data: JSON.stringify(object2)
                    });
                }
            }
        });
        //初始化
        zzpUtils.webSocket.init();
        laydate.render({
            elem: '#cjsj'
            ,range: true
        });
        //新增
        var zt;
        function add() {
            var data = $table.bootstrapTable('getData')
            console.log('66666',data.length);
            if(data.length==0){
                zzpUtils.dialog({
                    title: "新增补货单",
                    type: 2,
                    content: "/Replenishment/addReplenishment",
                    end: function () {//增加成功后刷新
                        zzpUtils.refreshTable("#table");
                    }
                });
            }else{
                for(var i=0;i<data.length;i++){
                    if(data[i].zt==0||data[i].zt==3){
                        alert("请把补货单据生成确认后才可以新增下一条补货单据");
                        break;

                    }else{
                        zzpUtils.dialog({
                            title: "新增补货单",
                            type: 2,
                            content: "/Replenishment/addReplenishment",
                            end: function () {//增加成功后刷新
                                zzpUtils.refreshTable("#table");
                            }
                        });
                    }
                }
            }
        }
        //查看
        function getDialog(data){
            var ids=[];
            ids.push(data.row.bhid);
            var zzpUtils = layui.zzpUtils,
                $ = layui.jquery,
                laydate = layui.laydate;
            zzpUtils.dialog({
                title: "查看明细",
                type: 2,
                content: "/Replenishment/selectReplenishment?ids=" + ids.join(","),
                //area: ['70%', '70%'], //设置dialog大小，也可以不设置，
            });
        }
        //查看补货任务
        function getBhrwDialog(data){
            var bhid=data.row.bhid;
            var sczt=data.row.sczt;
            console.log("8888888"+data.row);
            var zzpUtils = layui.zzpUtils,
                $ = layui.jquery,
                laydate = layui.laydate;
            zzpUtils.dialog({
                title: "查看补货任务",
                type: 2,
                content: "/Replenishment/searchReplenishment?bhid=" + bhid+"&sczt="+sczt,
                //area: ['70%', '70%'], //设置dialog大小，也可以不设置，
            });
        }
        //生成
        function generateDialog(data){
            var id=data.row.id;
            var zzpUtils = layui.zzpUtils,
                $ = layui.jquery,
                laydate = layui.laydate;
            zzpUtils.dialog({
                title: "下发补货任务",
                type: 2,
                content: "/Replenishment/createReplenishment?id=" + id,
                area: ['35%', '35%'], //设置dialog大小，也可以不设置，
            });
        }
        //编辑
        function upDialog(data) {
            var id=data.row.id;
            var bh=data.row.bh;
                zzpUtils.dialog({
                    title: "修改",
                    type: 2,
                    content: "/Replenishment/editReplenishment?id=" + id+"&bh="+bh,
                    area: ['40%', '40%'], //设置dialog大小，也可以不设置，
                    end: function () {//修改成功后刷新
                        zzpUtils.refreshTable("#table");
                    }
                });
        }
        //生成补货单
        function generateBhdDialog(data) {
            var $rows = $table.bootstrapTable('getSelections');
            var ids = $.map($rows, function (row) {
                return row.id;
            })
            zzpUtils.confirm("您将选择的数据生成补货单", function (index) {
                zzpUtils.action({
                    url: "/wmsartificialreplenishment/generateReplenishment",
                    data: JSON.stringify({ids: ids}),
                    success: function (data) {
                        if(data.message!=""){
                            layer.msg(data.message);
                        }else{
                            layer.msg(data.title);
                        }
                        zzpUtils.refreshTable("#table");
                    }
                })
            })
            return false;
        }
        //取消补货
        function cancelBhDialog(data) {
           var ids=data.row.id;
                zzpUtils.confirm("您将取消选择的数据", function (index) {
                    zzpUtils.action({
                        url: "/wmsartificialreplenishment/cancelBhd",
                        data: JSON.stringify({id:ids}),
                        success: function (data) {
                            if(data.message!=""){
                                layer.msg(data.message);
                            }else{
                                layer.msg(data.title);
                            }
                            zzpUtils.refreshTable("#table");
                        }
                    })
                })
                return false;
        }
        //删除
        function delDialog(ids) {
            zzpUtils.confirm("您将删除选择的数据", function (index) {
                zzpUtils.action({
                    url: "/wmsartificialreplenishment/delete",
                    data: JSON.stringify({ids: ids}),
                    success: function (data) {
                        if(data.message!=""){
                            layer.msg(data.message);
                        }else{
                            layer.msg(data.title);
                        }
                        zzpUtils.refreshTable("#table");
                    }
                })
            })
            return false;
        }
        laydate.render({
            elem: '#test5'
            ,theme: '#20a0ff'
        })
        var $table = zzpUtils.table({
            elem: '#table',
            isApi: true,
            isOperate: true,
            pagination: true,
            url: '/wmsartificialreplenishment/list',
            columns: [{
                field: '',
                checkbox: true
            }, {
                field: 'bh',
                title: '补货单号'
            },{
                field: 'cjsj',
                title: '创建时间'
            },{
                field: 'cjr',
                title: '创建人'
            },{
                field: 'scsj',
                title: '生成时间',
            },{
                field: 'qrsj',
                title: '确认时间'
            },{
                field: 'show_zt',
                title: '状态'
            },{
                field: 'show_sczt',
                title: '生成状态'
            },{
                field: 'bz',
                title: '补货单备注'
            }],
            queryParams: function () {//查询条件
                return  zzpUtils.getFormJson($("#searchForm"));
            },
            detailFormatter: function (index, row) {
                return "这里就没有确定过11";
            },
            /*定义表格右侧按钮*/
            operateFormatter: operateFormatter
        });
        /*表格右侧按钮*/
        function operateFormatter(value, row, index) {
            var button = '', buttons1 = '', buttons2 = '';
                for (var i = 0; i < functions.tableBtn.length; i++) {
                var item = functions.tableBtn[i];
                var buttons ='<button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="' + item.event + '">' + item.name + '</button>';
                    //更多之外
                if (item.event === 'getEvent') {
                    buttons1 +=buttons
                } else {
                    if(row.zt=="0"){
                        if (item.event == 'searchBhEvent'||item.event == 'editEvent'||
                            item.event == 'generateBhd'||item.event == 'deleteEvent') {
                            switch (item.event) {
                                case 'deleteEvent':
                                    buttons2 += '<button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="' + item.event + '">' + item.name + '</button>';
                                    break;
                                default:
                                    buttons2 += buttons;
                            }
                        }
                    }else if(row.zt=="1"){
                        if(row.sczt=="0"){
                            if (item.event == 'cancelBh'||item.event== 'generateEvent'||
                                item.event == 'searchBhEvent') {
                                buttons2+= buttons;
                            }
                        }else{
                            if (item.event == 'cancelBh'||
                                item.event== 'searchBhEvent') {
                                buttons2+=buttons;
                            }
                        }

                    }else{
                        if (
                            item.event== 'searchBhEvent') {
                            buttons2+= '<button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="' + item.event + '">' + item.name + '</button>';
                        }
                    }
                }
            }
            button += buttons1;
            button += buttons2;
            button += '</span>'
                + '</div>';
            return button;
        }
        window.queryTable = function (val) {
            var formdata = $("form").serializeArray();
            var Datas = {};
            var cjsjstart = '',cjsjend = '';
            formdata.forEach(function (item) {
                Datas[item.name] = item.value;
            });
            if (Datas.time) {
                var start = Datas.time.substring(0, 10);
                var end = Datas.time.substring(12);
                var timestart = new Date(start);
                cjsjstart = timestart.getTime();
                var timeend = new Date(end);
                cjsjend = timeend.getTime();
            }
            var DATA = {};
            for (var key in Datas) {
                if(key !== 'time') {
                    if (val === 1) {
                        DATA[key] = Datas[key];
                    }else {
                        DATA[key] = "";
                    }
                }
            }
            if (val === 1) {
                DATA.cjsj_start = cjsjstart;
                DATA.cjsj_end = cjsjend;
            }else {
                DATA.cjsj_start = "";
                DATA.cjsj_end = "";
            }
            DATA.page = 1;
            DATA.rows = 10;
            $('#table').bootstrapTable('refresh',{
                query: DATA
            })
        }
    });
</script>
</html>